package com.tianma315.web.tymenu.controller;


import java.util.Arrays;
import java.util.List;

import com.tianma315.core.common.utils.Result;
import com.tianma315.core.tymenu.domain.TyMenuDO;
import com.tianma315.core.tymenu.domain.TyMenuVO;
import com.tianma315.core.tymenu.service.TyMenuService;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 溯源档案菜单(环节)
 * </pre>
 * <small> 2020-02-24 10:29:16 | Aron</small>
 */
@Controller
@RequestMapping("/tymenu/tyMenu")
public class TyMenuController extends BaseController{
	@Autowired
	private TyMenuService tyMenuService;
	
	@GetMapping()
	@RequiresPermissions("tymenu:tyMenu:tyMenu")
	String TyMenu(){
	    return "tymenu/tyMenu/tyMenu";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("tymenu:tyMenu:tyMenu")
	public Result<Page<TyMenuDO>> list(Integer pageNumber, Integer pageSize, TyMenuDO tyMenuDTO){
		// 查询列表数据
        Page<TyMenuDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<TyMenuDO> wrapper = new EntityWrapper<TyMenuDO>(tyMenuDTO);
        page = tyMenuService.selectPage(page, wrapper);
        int total = tyMenuService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	@ResponseBody
	@GetMapping("/repeatList")
//	@RequiresPermissions("tymenu:tyMenu:tyMenu")
	public List<TyMenuVO> repeatList(Integer traceabilityFileId,Integer procedureLinkId){
		// 查询列表数据
		List<TyMenuVO> list	= tyMenuService.selectVOById(traceabilityFileId,procedureLinkId);
		return list;
	}
	/**
	 * 跳转添加数据页面
	 */
	@GetMapping("/addRepeat/{traceabilityFileId}/{proceduresLinkId}")
	public String addRepeat(@PathVariable("traceabilityFileId") Integer traceabilityFileId,@PathVariable("proceduresLinkId")Integer proceduresLinkId,Model model){
		model.addAttribute("traceabilityFileId",traceabilityFileId);
		model.addAttribute("proceduresLinkId",proceduresLinkId);
		return "trace/traceOut/addRepeat";
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/saveRepeat")
	public Result<String> saveRepeat(@RequestBody TyMenuVO tyMenuVO){
		tyMenuVO.setCreateBy(getUserId());
		if(tyMenuService.addRepeat(tyMenuVO)){
			return Result.ok();
		}
		return Result.fail();
	}

	
	@GetMapping("/add")
	@RequiresPermissions("tymenu:tyMenu:add")
	String add(){
	    return "tymenu/tyMenu/add";
	}

	@GetMapping("/edit/{menuId}")
	@RequiresPermissions("tymenu:tyMenu:edit")
	String edit(@PathVariable("menuId") Integer menuId,Model model){
		TyMenuDO tyMenu = tyMenuService.selectById(menuId);
		model.addAttribute("tyMenu", tyMenu);
	    return "tymenu/tyMenu/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("tymenu:tyMenu:add")
	public Result<String> save( TyMenuDO tyMenu){
		if(tyMenuService.insert(tyMenu)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("tymenu:tyMenu:edit")
	public Result<String>  update( TyMenuDO tyMenu){
		tyMenuService.updateById(tyMenu);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Integer menuId){
		if(tyMenuService.updateStaById(menuId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("tymenu:tyMenu:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] menuIds){
		tyMenuService.deleteBatchIds(Arrays.asList(menuIds));
		return Result.ok();
	}
	
}
